<template>
  <div id="changedialog">
    <commonformdialog
      :isshowDialogs="isshowDialog"
      :dialogTitle="dialogTitle"
      @closetextDialog="closetextDialog"
      @confirmbtn="confirmbtn"
      :dialogWidth="'750px'"
    >
      <div class="innerLoding">
        <el-row>
          <el-form
            :model="formData"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            size="small"
          >
            <el-row>
                <el-col :span="20">
                <el-form-item label="所在城市：" prop="city_id">
                  <el-cascader
                    ref="cascader"
                    filterable
                    placeholder="请选择所在城市"
                    v-model="formData.city_id"
                    :options="cityoptions"
                    :props="{
                      expandTrigger: 'hover',
                      emitPath: false,
                    }"
                    clearable
                     style="width: 100%"
                    :show-all-levels="false"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="线路：" prop="line_id">
                  <el-select
                    v-model="formData.line_id"
                    placeholder="请选择线路"
                    style="width: 100%"
                    filterable 
                    clearable
                      :disabled='!formData.city_id'
                  >
                    <el-option
                      v-for="item in prepareLineOptionArr"
                      :key="item.id"
                      :label="item.title"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="日期：" prop="start_date">
                  <el-date-picker
                    v-model="formData.start_date"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择开始日期"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="开始时间：" prop="start_time">
                  <el-time-picker
                    style="width: 100%"
                    value-format="HH:mm:ss"
                    v-model="formData.start_time"
                    placeholder="请选择开始时间"
                  >
                  </el-time-picker>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="结束时间：" prop="end_time">
                  <el-time-picker
                    style="width: 100%"
                    v-model="formData.end_time"
                    value-format="HH:mm:ss"
                    placeholder="请选择结束时间"
                  >
                  </el-time-picker>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="名额：" prop="num">
                  <el-input
                    placeholder="请输入可选名额"
                    type="number"
                    clearable
                    v-model="formData.num"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
      </div>
    </commonformdialog>
  </div>
</template>

<script type="text/javascript">
//引入弹窗
import commonformdialog from "@/components/commondialog/commondialog";
import uploadimg from "@/components/uploadimg/uploadimg";

import { timetableCreate, timetableUpdate,prepareLineOption } from "@/api/dataPreparation";
export default {
  data() {
    return {
      showPicker: false,
      multipleopen: true,
      prepareLineOptionArr: [],
      formData: {
        line_id: "",
        start_date: "",
        city_id: "",
        start_time: "",
        end_time: "",
        num: "",
      },
      rules: {
        city_id: [{ required: true, message: "请选择城市", trigger: "blur" }],
        line_id: [{ required: true, message: "请选择线路", trigger: "blur" }],
        start_date: [
          { required: true, message: "请选择日期", trigger: "blur" },
        ],
        start_time: [
          { required: true, message: "请选择开始时间", trigger: "blur" },
        ],
        end_time: [{ required: true, message: "请选择结束时间", trigger: "blur" }],
        num: [{ required: true, message: "请输入名额", trigger: "blur" }],
      },
    };
  },
  props: {
    //是否显示弹窗
    isshowDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
    //表单的名字
    dialogTitle: {
      type: String,
      default: "",
      required: true,
    },
    //是编辑还是新增
    formType: {
      type: Number,
      default: 0,
    },

    cityoptions: {
      type: Array,
    },
    dialogdata: {
      type: Object,
    },
  },
  components: {
    commonformdialog,
  },
  methods: {
 
    closetextDialog() {
      this.$emit("closedialog");
    },
 //获取下拉
    async getprepareLineOption(val) {
      let data = await prepareLineOption({ city_id: val });
      this.prepareLineOptionArr = data.data;
    },
    confirmbtn() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;
        let params = this.formData;
        let data;
        if (this.formType == 0) {
          data = await timetableCreate(params);
        } else {
          data = await timetableUpdate(params);
        }
        if (data.code == "0000") {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.$emit("confirm");
        }
      });
    },


  },
  watch: {
    isshowDialog: function (val) {
   this.getprepareLineOption(this.formData.city_id);
      if (!val) {
        this.$refs["ruleForm"].resetFields();
      }
    },
    dialogdata: function (val) {
      this.formData = val;
    },
     'formData.city_id':function(val){
            this.getprepareLineOption(val);
    }
  },
  created() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  #InputSearch {
    display: none !important;
  }
  .grid-emojis {
    grid-template-columns: repeat(10, 10%) !important;
  }
  .small {
    position: absolute;
    z-index: 9;
    left: -45px;
    top: 30px;
  }
}
</style>
